import { Component, OnInit } from '@angular/core'
import { JsonApiService } from 'app/core/api/json-api.service' //http请求配置引入
import { Router, ActivatedRoute } from '@angular/router' //路由引入
import { config } from '../../shared/smartadmin.config'

declare var $: any
@Component({
  selector: 'sa-bom-material-all',
  templateUrl: './bom-material-all.component.html',
  styleUrls: ['./bom-material-all.component.css']
})
export class BomMaterialAllComponent implements OnInit {
  tableData: any = [] // table的表单数据
  totalCounts: number = 0 //分页总条数
  curPage: number = 1 //分页当前页
  pageSize: number = 10 //内置的分页每页条数
  // 查询参数
  bomNumber: string = '' // BOM编码
  specifications: string = ''

  materielType: any = '' // 产品类型(数字)
  materielName: string = '' // 产品名称
  materielNumber: string = '' // 物料编码

  constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
    activatedRoute.queryParams.subscribe(queryParams => {})
  }
  ngOnInit() {
    this.queryMaterial() //获取物料类型列表数据
    this.load()
  }

  //初始化加载list数据
  load() {
    let param = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        materielType: this.materielType,
        materielName: this.materielName, // 产品名称
        materielNumber: this.materielNumber, // 物料编码
        specifications: this.specifications // 录入人
      }
    }
    let reqdata = 'gridPager=' + JSON.stringify(param)
    this.jsonApiService.http('materiel/v3/queryMaterielV3PageList.html', reqdata).subscribe(result => {
      if (result.isSuccess && result.exhibitDatas) {
        this.tableData = result.exhibitDatas
        this.totalCounts = result.recordCount
      }
    })
  }
  // 删除输入
  delKey(type) {
    if (type == 'materielName') {
      this.materielName = ''
    }
    if (type == 'materielNumber') {
      this.materielNumber = ''
    }
    if (type == 'specifications') {
      this.specifications = ''
    }
  }

  // 查询
  tableSearch() {
    this.curPage = 1
    this.load()
  }
  //分页回调
  paginate(e) {
    this.curPage = e.page + 1
    this.pageSize = e.rows
    this.load()
  }
  // 物料实体 往后台传递的
  material: any = {
    id: '',
    materielType: '',
    materielTypeStr: '',
    materielName: '',
    materielAlias: '',
    materielNumber: '',
    specifications: '',
    sensorChannel: '',
    version: '',
    sim: '',
    unit: ''
  }
  validatorOptions = {
    excluded: [':disabled'],
    feedbackIcons: {
      valid: 'glyphicon glyphicon-ok',
      invalid: 'glyphicon glyphicon-remove',
      validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
      materielType: {
        validators: {
          notEmpty: {
            message: '请选择物料类型'
          }
        }
      },
      materielName: {
        validators: {
          notEmpty: {
            message: '请填写物料名称'
          }
        }
      },
      materialCode: {
        validators: {
          notEmpty: {
            message: '请填写物料编码'
          }
        }
      },
      specifications: {
        validators: {
          notEmpty: {
            message: '请填写规格型号'
          }
        }
      },

      sensorChannel: {
        validators: {
          notEmpty: {
            message: '请填写通道'
          }
        }
      },
      version: {
        validators: {
          notEmpty: {
            message: '请填写版本号'
          }
        }
      },
      sim: {
        validators: {
          notEmpty: {
            message: '请填写贴片卡'
          }
        }
      },

      unit: {
        validators: {
          notEmpty: {
            message: '请填写单位'
          }
        }
      }
    }
  }
  // 选择物料类型
  typeChange(value) {
    const material = this.material
    material.materielType = value
    const type = this.materialTypeList.find(el => el.materielType === value)
    material.materielTypeStr = type.materielTypeName
  }

  // 更新dom编码
  updata_domCode(row, modal) {
    $('#bomHoldFrom').data('bootstrapValidator').resetForm()
    let obj = JSON.parse(JSON.stringify(row))
    for (const key in this.material) {
      if (Object.prototype.hasOwnProperty.call(this.material, key)) {
        this.material[key] = obj[key] || ''
      }
    }
    modal.show()
  }
  // 删除dom编码
  del_domCode(row) {
    this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除').then(() => {
      let reqdata = 'id=' + row.id
      this.jsonApiService.http('materiel/v3/deleteMaterielV3ById.html', reqdata).subscribe(data => {
        this.jsonApiService.message(data.message)
        if (data.success) {
          this.load()
        }
      })
    })
  }
  // 刷新表
  refresh() {
    this.load()
  }
  // 关闭添加和编辑弹窗
  closeAddBomModal(modal) {
    $('#bomHoldFrom').data('bootstrapValidator').resetForm()
    modal.hide()
    this.clearData()
  }
  clearData() {
    this.material = {
      id: '',
      materielType: '',
      materielTypeStr: '',
      materielName: '',
      materielAlias: '',
      materielNumber: '',
      specifications: '',
      sensorChannel: '',
      version: '',
      sim: '',
      unit: ''
    }
    $('#bomHoldFrom').data('bootstrapValidator').resetForm()
  }
  // 提交添加的数据
  submitModal(modal) {
    $('#bomHoldFrom').bootstrapValidator('validate') //开启验证
    let isValid = $('#bomHoldFrom').data('bootstrapValidator').isValid() //验证是否通过
    if (isValid) {
      this.jsonApiService.http_obj('materiel/v3/editMaterielV3.html', this.material).subscribe(data => {
        this.jsonApiService.message(data.message)
        if (data.success) {
          //
          this.load()
          this.closeAddBomModal(modal)
        }
      })
    }
  }

  materialTypeList: any = [] //物料类型列表数据
  //请求物料类型的接口
  queryMaterial() {
    this.jsonApiService.http('materialType/list.html', '').subscribe(data => {
      if (data.success) {
        this.materialTypeList = data.data
      }
    })
  }
}
